<?php
class ShareAction extends Action {	
	var $model;
	function _initialize(){
		$this->model = D('Share','home');

		$this->setTitle( L('page_head_title') );

	}
	//收藏
	function collect(){
		$this->index('collect');
	}
	//我的分享
	function my(){
		$this->index('my');
	}
	//大家的分享
	function all(){
		$this->index('all');
	}
			
	function index($type){		
		$map['isDel'] = 0;
		if ($type=='collect') {
			$map['uid']   = $this->mid;
			$map['type']  = 2;
		}elseif ($type=='my'){
			$map['uid']   = $this->mid;
			$map['type']  = array('NEQ',2);	
		}else{
			$map['type']  = array('NEQ',2);				
		}


		$result = $this->model->getShareList($map);
		$this->assign('data',$result['data']);
		$this->assign('html',$result['html']);
		$this->assign('count',$result['count']);
			
		$this->display($type);
	}
	
	function doAddShare() {	
		$map['uid']     = $this->mid;
		$map['type']    = $_POST['type'];
		$map['temp']    = $_POST['temp'];
		$map['aimId']   = intval($_POST['aimId']);
		/*取消分享一次限制
		 * $test = $this->model->where($map)->find();
		if($test){
			echo -1; exit;
		}*/
						
		$map['userName'] = getUserName($this->mid);
		$map['fromUid'] = intval($_POST['fromUid']);
		$map['fromUserName'] = getUserName($map['fromUid']);
		$map['info']    = h($_POST['info']);		     		
		$map['title']   = h($_POST['title']);
		$map['toids']	= $_POST['toids'];
		$map['cTime']   = time(); 
		
		$data = $this->_getData();
		if(isset($_POST['intro'])){
			$data['intro']  = msubstr($_POST['intro'],0,100);
		}		

		$map['data']    = serialize($data);

        $result = $this->model->add($map);
		if($result){
			//发送通知和动态信息
			$this->_sendMessage($map,$data);
			echo 1;
		}else{
			echo 0;
		}
	}
	
	function _sendMessage($map,$data){		
		$data['title']   = $map['title'];
		$data['info']    = $map['info'];
		$data['aimId']   = $map['aimId'];
		$data['fromUid'] = $map['fromUid'];
		
		$body  = $data;
		$title = $data;
		
		unset($title['info']);
		unset($body['title']);
		
		$gid     = isset($data['gid']) ? $data['gid'] : 0;
		$private = isset($data['private']) ? $data['private'] : 0;
		$fri_ids = $_POST['fids'];
		
		if($data['isGroup'] && !empty($gid)){
			if(empty($fri_ids)){
				$fri_ids = $this->_getGroupMember($gid);
				$fri_ids = implode(',',$fri_ids);
			}

			return Service('Notify')->put($map['temp'],$fri_ids,$title,$body,'','',$gid);
		}
		
		if($map['type']='0'){
			$res = Service('Feed')->put($map['temp'],$title,$body,'','',$gid,$private);
		}	

		if($map['type']=='1' && !empty($fri_ids)){
			$res = Service('Notify')->put($map['temp'],$fri_ids,$title,$body,'','',$gid);
		}

		return $res;	
	}
	
	function _getData(){
		$data = $_POST['data'];
		if(empty($data)){
			return '';
		}
		
		$dataArr = explode(',', $data);
		foreach ($dataArr as $v){
			$a = explode(':', $v);
			$key = $a[0];
			$val = $a[1];
			
			if(!empty($val) && $key!='') $arr[$key] = $val;
		}
		
		return $arr;
	}
	
	function delShare(){
		$id = intval($_POST['id']);
		if(empty($id)) {echo 0;exit;}

		$res = $this->model->where("id='$id'")->delete();
		//dump($res);dump($this->model->getLastSql());
		if($res) echo 1;
		else echo 0;
	}
	
	function _getGroupMember($gid){
		$map['gid'] = $gid;
		$list = D('Member', 'group')->where($map)->field('uid')->findall();	
		
		return getSubByKey($list, 'uid');	
	}
	function getShowToNames(){
		$ids = $_POST['ids'];
		$arr = explode(",",$ids);
		$str="";
		foreach($arr as $val){
			$str.=getUserName($val);
			$str.="/";
		}
		$str=substr($str,0,$str.length-1);
		echo $str;
	}
}
?>